package com.alumni_circle.controller.home.addition;

import cn.hutool.core.util.ObjectUtil;
import com.alumni_circle.core.config.SysLog;
import com.alumni_circle.core.entity.BaseId;
import com.alumni_circle.core.entity.ResponseModel;
import com.alumni_circle.core.utils.SecurityUtils;
import com.alumni_circle.core.web.BaseController;
import com.alumni_circle.entity.home.addition.LikeArticleSum;
import com.alumni_circle.service.home.addition.impl.LikeArticleSumServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 龙小虬
 * @since 2020-08-30 22:37
 */
@RestController
@RequestMapping("/likeArticleSum")
public class LikeArticleSumController extends BaseController<LikeArticleSum> {

    @Resource
    LikeArticleSumServiceImpl likeArticleSumService;

    /**
     * 查询是否点赞文章
     *
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getLikeArticle", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询是否点赞文章", notes = "查询是否点赞文章", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询是否点赞文章",url="/likeArticleSum/getLikeArticle")
    public ResponseModel getLikeArticle(@ApiParam(required = true, name = "baseId")
                                    @RequestBody BaseId baseId) {
        QueryWrapper<LikeArticleSum> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",SecurityUtils.getId());
        queryWrapper.eq("article_id",baseId.getId());
        if(ObjectUtil.hasEmpty(likeArticleSumService.getOne(queryWrapper))){
            return new ResponseModel<>(false);
        }
        return new ResponseModel<>(true);
    }

    /**
     * 更新文章点赞
     *
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateLikeArticle", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "更新文章点赞", notes = "更新文章点赞", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="改",accessInfo="更新文章点赞",url="/likeArticleSum/updateLikeArticle")
    public ResponseModel updateLikeArticle(@ApiParam(required = true, name = "baseId")
                                    @RequestBody BaseId baseId) {
        QueryWrapper<LikeArticleSum> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",SecurityUtils.getId());
        queryWrapper.eq("article_id",baseId.getId());
        LikeArticleSum likeArticle = likeArticleSumService.getOne(queryWrapper);
        if(ObjectUtil.hasEmpty(likeArticle)){
            LikeArticleSum likeArticleSum = new LikeArticleSum();
            likeArticleSum.setArticleId(baseId.getId());
            likeArticleSum.setId(SecurityUtils.getId());
            likeArticleSumService.save(likeArticleSum);
            // 增加
            return new ResponseModel<>(true);
        }
        // 不增加
        likeArticleSumService.removeById(likeArticle.getId());
        return new ResponseModel<>(false);
    }

}
